package com.ccs.loas;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class CreateLOA {
	
	public String create(WebDriver driver, String coverage) throws Exception{
		
		driver.manage().window().maximize();
		Miscellaneous m = new Miscellaneous();
		
		WebDriverWait wait = new WebDriverWait(driver,30);
	    wait.until(
	        ExpectedConditions.elementToBeClickable(
	             By.xpath("//div[@id='mainContent']/div[4]/nav/ul/li[2]")));
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		m.go_to_Loa(driver);
		m.create_button_loa(driver);
		
		//SEARCH CREATE LOA
		Select loc = new Select(driver.findElement(By.xpath("//select[@id='locationSearch']")));
		loc.selectByVisibleText("Global City");
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//div[@id='addCreateLoaDialog']/div/div/fieldset/button[1]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		Thread.sleep(10000);
		
        //VERIFICATION OF TABLE HEADER
        String admissionDate = driver.findElement(By.xpath("//table[@id='refLoaTable']/thead/tr/th[1]")).getText();
        String patientName = driver.findElement(By.xpath("//table[@id='refLoaTable']/thead/tr/th[2]")).getText();
        String confinementNo = driver.findElement(By.xpath("//table[@id='refLoaTable']/thead/tr/th[3]")).getText();
        String pin = driver.findElement(By.xpath("//table[@id='refLoaTable']/thead/tr/th[4]")).getText();
        confinementNo = confinementNo.replaceAll(" ", "");
        
        if(admissionDate.equals("Admission Date") && patientName.equals("Patient Name") && confinementNo.equals("ConfinementNumber") && pin.equals("PIN")){
        	System.out.println("LOA Search Table Header: Verified OK");
        }else{
        	System.out.println("LOA Search Table Header: Verified NOT OK");
        }
		
        //SELECT LOA
        Actions actions = new Actions(driver);
        List<WebElement> row = driver.findElements(By.xpath("//table[@id='refLoaTable']/tbody/tr"));
		int size = row.size();
        System.out.println("Total number for LOA creation: "+size);

        String pName = driver.findElement(By.xpath("//table[@id='refLoaTable']/tbody/tr[1]/td[2]/div")).getText();
        //String pName = driver.findElement(By.xpath("//div[@id='trPatientName-0']")).getText();
        
        System.out.println(pName);
        WebElement mouseover2 = driver.findElement(By.xpath("//table[@id='refLoaTable']/tbody/tr[1]"));
        actions.moveToElement(mouseover2);
        actions.click().perform();
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
        
        //VERIFY LOA PAGE
        System.out.println("VERIFYING LOA HEADER DETAILS:");
        String patientPin = driver.findElement(By.xpath("//div[@id='headerWrapper']/div[1]/strong[1]")).getText();
        String birthDate =  driver.findElement(By.xpath("//div[@id='headerWrapper']/div[1]/div[1]/strong[1]")).getText();
        String gender =  driver.findElement(By.xpath("//div[@id='headerWrapper']/div[1]/div[1]/strong[2]")).getText();
        String roomAndBed =  driver.findElement(By.xpath("//div[@id='subHeaderWrapper']/div[1]/strong[1]")).getText();
        String admDate =  driver.findElement(By.xpath("//div[@id='subHeaderWrapper']/div[1]/strong[2]")).getText();
        String admType =  driver.findElement(By.xpath("//div[@id='subHeaderWrapper']/div[1]/strong[3]")).getText();
        String confinementNumber =  driver.findElement(By.xpath("//div[@id='subHeaderWrapper']/div[2]/strong[1]")).getText();
        String roomRate =  driver.findElement(By.xpath("//div[@id='subHeaderWrapper']/div[2]/strong[2]")).getText();
        String loa_array[] = { patientPin, birthDate, gender, roomAndBed, admDate, admType, confinementNumber, roomRate};
        
        for(int x=0; x<loa_array.length ;x++){
        	if(loa_array[x].isEmpty()||loa_array[x]==""){
        		 	String array_description;
        		 	switch (x) {
        	            case 1:  array_description = "Patient PIN";
        	                     break;
        	            case 2:  array_description = "BirthDate";
        	                     break;
        	            case 3:  array_description = "Gender";
        	                     break;
        	            case 4:  array_description = "Room and Bed";
        	                     break;
        	            case 5:  array_description = "Admission Date";
        	                     break;
        	            case 6:  array_description = "Confinement Number";
        	                     break;
        	            case 7:  array_description = "Room Rate";
        	                     break;
        	            default: array_description = "Invalid month";
        	                     break;
        	        }
        	        System.out.println("Empty String found in: "+array_description);
        	}
        }
        
        //IS START OF COVERAGE = ADMISSION DATE?
        String date_coverage = driver.findElement(By.xpath("//input[@id='startCoverage']")).getAttribute("value");
        if(date_coverage.equals(admDate)){
        	System.out.println("Start of Coverage is equals to Admission Date! CORRECT");
        }else{
        	System.out.println("Start of Coverage is not equals to Admission Date! INCORRECT");
        }
        
        //IS LOA DATE = CURRENT DATE?
        DateFormat cd = new SimpleDateFormat("MM/dd/yyyy");
        Date current = Calendar.getInstance().getTime();
		String dateToday = cd.format(current);
        String loa_date = driver.findElement(By.xpath("//form[@id='loaForm']/div[2]/div[1]/strong")).getText();
        String array_month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        loa_date = loa_date.replaceAll(",", "");
        String new_loa_date[] = loa_date.split(" "); 
        
        int convertMonth=0;
        for(int y=0; y<array_month.length;y++){
        	if(new_loa_date[0].equals(array_month[y])){
        		y++;
        		convertMonth = y;
        		break;
        	}
        }

        String stringMonth="";
        if(convertMonth<10){
        	stringMonth = Integer.toString(convertMonth);
        	stringMonth = "0"+stringMonth;
        }else{
        	stringMonth = Integer.toString(convertMonth);
        }
        
        int intDays= Integer.parseInt(new_loa_date[1]);
        String stringDay="";
        if(intDays<10){
        	stringDay = Integer.toString(intDays);
        	stringDay = "0"+stringDay;
        }else{
        	stringDay = new_loa_date[1];
        }
        String final_date = stringMonth+"/"+stringDay+"/"+new_loa_date[2];
        if(final_date.equals(dateToday)){
        	System.out.println("Loa Date is equals to current date. CORRECT");
        }else{
        	System.out.println("Loa Date is not equals to current date. INCORRECT");
        }
        
        //FILL-UP LOA
		String code = m.random_number();
		String final_code = "cocoloa"+code;
		driver.findElement(By.xpath("//input[@id='corpLoaNo']")).sendKeys(final_code);
		
		//DATE PICKER
		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		Date today = Calendar.getInstance().getTime();
		String reportDate = df.format(today);
		String[] arrString = reportDate.split("/");
		@SuppressWarnings("unused")
		String year = arrString[0];
		String month = arrString[1];
		String day = arrString[2];
		System.out.println("Current Date: "+reportDate);
		@SuppressWarnings("unused")
		int intDay = Integer.parseInt(day);
		int intMonth = Integer.parseInt(month);
		intMonth++;
		month = Integer.toString(intMonth);
		
		driver.findElement(By.xpath("//input[@id='endCoverage']")).click();
		Select calendarMonth = new Select(driver.findElement(By.xpath("//select[@class='ui-datepicker-month']")));
		calendarMonth.selectByValue(month);
		
        WebElement mouseover1 = driver.findElement(By.xpath("//table[@class='ui-datepicker-calendar']/tbody/tr[1]/td[3]/a"));
        actions.moveToElement(mouseover1);
        actions.click().perform();
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
		
		WebElement coverageAmount = driver.findElement(By.xpath("//input[@id='forMaxlimit']"));
		if(coverage.equals("covered")){
			coverageAmount.sendKeys("5000");
		}else{
			coverageAmount.sendKeys("1000");
			//driver.findElement(By.xpath("//input[@id='phRequired']")).click();
		}
		driver.findElement(By.xpath("//input[@id='roomAllowedId']")).sendKeys("Regular Private");
		driver.findElement(By.xpath("//input[@id='forRmAllowedRate']")).sendKeys("1500");
		driver.findElement(By.xpath("//input[@id='incPf']")).click();
		driver.findElement(By.xpath("//input[@id='forMaxPf']")).sendKeys("1500");
		driver.findElement(By.xpath("//table[@id='doctorList']/tbody/tr/td[3]/input")).click();
		driver.findElement(By.xpath("//input[@id='txt-0']")).sendKeys("1500");
		
		//SAVING AS DRAFT then NO
		driver.findElement(By.xpath("//input[@value='Save As Draft']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //Deny By Clicking NO
		driver.findElement(By.xpath("//button[contains(.,'No')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		m.loa_buttons(driver, "draft", "");
		final_code = patientPin+ "," +final_code;
		return final_code;
	}
	
}
